package com.itheima.mm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mm.pojo.PageResult;
import com.itheima.mm.pojo.permission.GroupPermission;
import com.itheima.mm.dto.Permissions;
import com.itheima.mm.service.GroupService;
import com.itheima.mm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/permissions")
public class GroupController {
    @Autowired
    public PermissionService permissionService;
    @Autowired
    public GroupService groupService;

    //权限组修改
    @PutMapping("/{id}")
    public String motify(@PathVariable Integer id, @RequestBody Permissions permissions) {

        groupService.updatepss(id, permissions);

        return "修改成功";
    }

    @GetMapping
    public PageResult selectList(Integer page, Integer pagesize, String keyword) {
        IPage <GroupPermission> p = new Page <>(page, pagesize);
        if (keyword == null) {

            IPage <GroupPermission> page1 = groupService.page(p, null);

            return new PageResult(page1.getTotal(), page1.getSize(), page1.getPages(),
                    page1.getCurrent(), page1.getRecords());

        }
        LambdaQueryWrapper <GroupPermission> warpper = new LambdaQueryWrapper <>();
        warpper.like(GroupPermission::getName, keyword);
        IPage <GroupPermission> page1 = groupService.page(p, warpper);

        return new PageResult(page1.getTotal(), page1.getSize(), page1.getPages(),
                page1.getCurrent(), page1.getRecords());
    }

    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Integer id) {


        return groupService.deleteById(id);
    }

    @PostMapping
    public String add(@RequestBody Permissions permissions) {

        return groupService.add(permissions);

    }

    //4.5权限组简单列表
    @GetMapping("/simple")
    public PageResult simple(String keyword) {
        LambdaQueryWrapper <GroupPermission> warpper = new LambdaQueryWrapper <>();
        warpper.like(GroupPermission::getName, keyword);
        List <GroupPermission> list = groupService.list(warpper);
        List <Permissions> permissionsList = new ArrayList <>();
        if (permissionsList == null) {
            return new PageResult();
        }
        for (GroupPermission groupPermission : list) {
            Permissions permissions = new Permissions();
            permissions.setId(groupPermission.getId());
            permissions.setTitle(groupPermission.getName());
            permissionsList.add(permissions);
        }
        PageResult pageResult = new PageResult();
        pageResult.setItems(permissionsList);
        return pageResult;
    }

    //4.6权限组详情
    @GetMapping("/{id}")
    public Permissions select(@PathVariable Integer id) {

        return groupService.selectById(id);
    }

}
